<?php
/**
 * zip压缩文件并下载
 * @author zhangjie@star-net.cn
 * @copyright © ktvme. All Rights Reserved.
 * @version $Id: zip.php 3 2017-05-08 03:02:47Z chendanming $
 */

namespace tbszip;

use curl\CurlUtil;
class zip {
    /**
     * 压缩文件并下载
     * @param array $arrfilename
     * @param string $outputfilename
     * $arrfilename = [
     *  ['filename','filename','ext'],
     *  ['filename','filename','ext'],
     * ]
     */
    function output(array $arrfilename,$outputfilename='download'){
        $zip = new clsTbsZip();
        $zip->CreateNew();
        $curl = CurlUtil::getInstance();
        if(!is_array($arrfilename[0])){//仅一个文件
            if(!filter_var ($arrfilename[0], FILTER_VALIDATE_URL )){//目录
                $a = pathinfo($arrfilename[0]);
                $zip->FileAdd($a['filename'].'.'.$a['extension'],$arrfilename[0],TBSZIP_FILE);
            }else{//网络资源
                $a = $curl->get($arrfilename[0]);
                $zip->FileAdd($arrfilename[1].'.'.$arrfilename[2],$a,TBSZIP_STRING);
            }
        }else{
            foreach($arrfilename as $k => $v){
                if(!filter_var ($v[0], FILTER_VALIDATE_URL )){//目录
                    $a = pathinfo($v[0]);
                    $zip->FileAdd($a['filename'].'.'.$a['extension'],$v[0],TBSZIP_FILE);
                }else{//网络资源
                    $a = $curl->get($v[0]);
//                    $error = json_decode($a,true);
//                    if(!isset($error['error'])){
//                        echo 1;
                        $zip->FileAdd($v[1].'.'.$v[2],$a,TBSZIP_STRING);
//                    }
//                    var_dump($a);

                }
            }
        }
//        $zip->Flush(TBSZIP_FILE, RUNTIME_PATH.'archive1_new.zip');
        // apply modifications as an HTTP downloaded file
        //        $zip->Flush(TBSZIP_DOWNLOAD, 'download.zip');
        $zip->Flush(TBSZIP_DOWNLOAD, $outputfilename.'.zip', 'application/zip'); // with a specific Content-Type

        // apply modifications as a downloaded file with your customized HTTP headers
        //        header("Content-type: application/force-download");
        //        header("Content-Disposition: attachment; filename=download.zip");
        //        $zip->Flush(TBSZIP_DOWNLOAD+TBSZIP_NOHEADER);
        $zip->Close();
    }
}